/*
 * yauosk - Yet Another Useless Operating System Kernel
 *
 * Copyright (c) 2009-2010 Matteo Cicuttin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "elf.h"
#include "x86.h"

#define SECTSIZE    512
#define ELFHDR      ((struct elfhdr*) 0x10000)

void disk_read_sector(void *, unsigned int);
void disk_read(unsigned int, unsigned int, unsigned int);
void disk_wait_ready(void);

void
disk_read_sector(void *loadaddr, unsigned int offset)
{
    disk_wait_ready();
    outb(0x1F2, 1);   // count = 1
    outb(0x1F3, offset);
    outb(0x1F4, offset >> 8);
    outb(0x1F5, offset >> 16);
    outb(0x1F6, (offset >> 24) | 0xE0);
    outb(0x1F7, 0x20);  // cmd 0x20 - read sectors

    // wait for disk to be ready
    disk_wait_ready();

    // read a sector
    insl(0x1F0, loadaddr, SECTSIZE/4);
}

void
disk_read(unsigned int dest, unsigned int count, unsigned int offset)
{
    unsigned int end;
    end = dest + count;
    dest &= ~(SECTSIZE-1);
    offset = (offset / SECTSIZE) + 1;
    
    while (dest < end)
    {
        disk_read_sector((unsigned char *)dest, offset);
        dest += SECTSIZE;
        offset++;
    }
}

void
disk_wait_ready()
{
    while((inb(0x1F7) & 0xC0) != 0x40)
        ;
}

void loader(void)
{
    struct proghdr *ph, *eph;
    
    /*char *p = (char *) 0xb8000;
    *p = 'C'; p += 2;
    *p = ' '; p += 2;
    *p = 'r'; p += 2;
    *p = 'u'; p += 2;
    *p = 'l'; p += 2;
    *p = 'e'; p += 2; 
    *p = 'z'; p += 2;*/
    
    disk_read((unsigned int) ELFHDR, SECTSIZE*8, 0);

    if(ELFHDR->magic != ELF_MAGIC)
        goto bad;
    
    ph = (struct proghdr*) ((unsigned char*) ELFHDR + ELFHDR->phoff);
    eph = ph + ELFHDR->phnum;
    for(; ph < eph; ph++)
        disk_read(ph->va, ph->memsz, ph->offset);

  ((void(*)(void)) (ELFHDR->entry))();

bad:
 // outw(0x8A00, 0x8A00);
  //outw(0x8A00, 0x8E00);
    while(1)
        ;
}